package com.css.apps.base.role.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.json.OrgJsonProvider;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.user.action.DirUserRole;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.db.util.StringHelper;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DirRoleUser extends AdminAction {
	
	private static Log log = LogFactory.getLog(DirUserRole.class);
	private String roleId;
	private String orgId;
	private String userName;
	private Page page;
	private String tree;

	public DirRoleUser() {
		page = new Page();
		page.setCurrentPage(1);
		page.setPageSize(10);
	}

	@SuppressWarnings("unchecked")
	protected String adminGo() {
		try {
			SRole role = QueryCache.get(SRole.class, roleId);
			if (role == null) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.failed"));
				return Action.ERROR;
			}
			List<String> list = new QueryCache("select a.userId from SUserRole a where a.roleId=:roleId")
					.setParameter("roleId", roleId).list();
			List<SUser> userList = getUserList(list);
			page.setResults(userList);
			// 设置分页
			loadPage(page);
			OrgJsonProvider json = new OrgJsonProvider();
			tree = json.getOrgRadio(null).toString();
			return SUCCESS;
		} catch (Exception ex) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	private void loadPage(Page page) {
		List<?> results = page.getResults();
		if (results != null && results.size() > 0) {
			page.setTotalRows(results.size());
			page.setTotalPages((int) Math.ceil(results.size() * 1.0 / page.getPageSize()));
			int total = results.size();
			int curPage = page.getCurrentPage();
			int pageSize = page.getPageSize();
			int startIndex = (curPage > 0 ? curPage - 1 : 0) * pageSize;
			if (startIndex > total) {
				page.setTotalRows(0);
				page.setResults(null);
				return;
			}
			int endIndex = startIndex + pageSize;
			if (endIndex > total) {
				endIndex = total;
			}
			results = results.subList(startIndex, endIndex);
			page.setResults(results);
		}
	}

	/**
	 * 此处查询未考虑多组织机构情况
	 */
	@SuppressWarnings("unchecked")
	private List<SUser> getUserList(List<String> userIdList) {
		List<SUser> resList = new ArrayList<>(), userList = QueryCache.idToObj(SUser.class, userIdList);
		boolean orgIsNotEmpty = StringHelper.isNotEmpty(orgId), nameIsNotEmpty = StringHelper.isNotEmpty(userName);
		for (SUser user : userList) {
			// if(BaseEnvironment.USERTYPE_NORMAL.equals(user.getUserType())) {
			if (orgIsNotEmpty || nameIsNotEmpty) {
				if (orgIsNotEmpty && nameIsNotEmpty && user.getOrgId().equals(orgId)
						&& (user.getLoginName().contains(userName) || user.getRealName().contains(userName))) {
					resList.add(user);
				} else if (orgIsNotEmpty && user.getOrgId().equals(orgId)) {
					resList.add(user);
				} else if (nameIsNotEmpty
						&& (user.getLoginName().contains(userName) || user.getRealName().contains(userName))) {
					resList.add(user);
				}
			} else {
				resList.add(user);
			}
			// }
		}
		return resList;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}
